package com.sjg.sys.listener;

import com.sjg.sys.entity.SysLoginRecord;
import com.sjg.sys.entity.SysUser;
import com.sjg.sys.service.SysLoginRecordService;
import com.sjg.sys.utils.SysConstant;
import com.sjg.sys.utils.springUtils.SpringContextUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.*;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>@Title  session创建、销毁、属性修改监听</p>
 * <p>@Description  描述</p>
 * <p>@Version 1.0.0 版本号</p>
 * <p>@author fuqingjian 作者</p>
 * <p>@date 2017/3/16 创建日期</p>
 */
public class SysSessionHandlerListener implements HttpSessionAttributeListener {

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        String name = event.getName();

        if(name.equals(SysConstant.SESSION_ATTR_USER)){
            SysUser user = (SysUser) event.getValue();
            //如果该用户已经登陆就让把他剔除或者失效
            if(SysConstant.onLineUsers.containsKey(user.getLoginName())){
                HttpSession session = SysConstant.onLineUsers.get(user.getLoginName());
                session.removeAttribute(user.getLoginName());
                session.invalidate();//销毁session
            }
            //将密码置空
            user.setLoginPwd(null);
            //将最新登陆用户session添加到缓存中
            SysConstant.onLineUsers.put(user.getLoginName(), event.getSession());
            SysConstant.sessionCount++;
            SysLoginRecordService sysLoginRecordService= SpringContextUtil.getBean("sysLoginRecordService");
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                    .getRequest();
            SysLoginRecord loginRecord=new SysLoginRecord();
            loginRecord.setLoginName(user.getLoginName());
            loginRecord.setName(user.getName());
            // 请求的IP
            String ip = request.getRemoteAddr();
            if("0:0:0:0:0:0:0:1".equals(ip)){
                ip = "127.0.0.1";
            }
            loginRecord.setLoginIp(ip);
            loginRecord.setLoginStatus(1);//登录
            loginRecord.setLoginTime(new Timestamp(new Date().getTime()));
            sysLoginRecordService.insert(loginRecord);
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        String name = event.getName();
        if (name.equals(SysConstant.SESSION_ATTR_USER)) {
            SysUser user = (SysUser) event.getValue();
            SysConstant.onLineUsers.remove(user.getLoginName());
            SysLoginRecordService sysLoginRecordService= SpringContextUtil.getBean("sysLoginRecordService");
            SysLoginRecord loginRecord=new SysLoginRecord();
            loginRecord.setLoginName(user.getLoginName());
            loginRecord.setName(user.getName());
            // 退出时没有请求的话就无法拿到IP
            String ip = "";
            loginRecord.setLoginIp(ip);
            loginRecord.setLoginStatus(2);//退出
            loginRecord.setLoginTime(new Timestamp(new Date().getTime()));
            sysLoginRecordService.insert(loginRecord);
        }
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
    }
}